<template>
	<om-formsub-outer :index="index">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				initValue: {},
				autoformdata: [],
				cachename: '',
				index: null,
				datas: [],
				order: 0,
				formdata: {},
				postId: "",
				dutyOffCacheKey: "adm-meet-summary-data-info-title5-dutyoff-cache",
				evaluateOffCacheKey: "adm-meet-summary-data-info-title5-evaluateoff-cache",
				assistOffCacheKey: "adm-meet-summary-data-info-title5-assistoff-cache",
				busCode:'',
				bodyUserId:'',
				defaultDutyOffDataCache:'adm-meet-summary-data-info-title5-defaultDutyOff-cache',
				evaMultiOffDefaultDataCache:'adm-meet-summary-data-info-title5-defaultevaMultiOff-cache',
				canEdit:true,
				evaMultiOffCacheKey:'adm-meet-summary-data-info-title5-evaMultiOff-cache'
			}
		},
		methods: {
			inputScore(val){
				let _that = this
				if(Number(val) > 10){
					_that.$refs.autoform.formdata.score = 10
				}
				if(Number(val) < 0){
					_that.$refs.autoform.formdata.score = 0
				}
				if( Number(val) >= 0 &&  Number(val)<= 10){
					_that.$refs.autoform.formdata.score = Number(val).toFixed(1)
				}
				if(!val){
					_that.$refs.autoform.clear("title5",'score')
				}
			},
			clearEvaMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title5","evaMultiOffId")
				uni.removeStorage({
					key:_that.evaMultiOffDefaultDataCache
				})
			},
			getMultiEvaUserResponse(userIdPosId){
				let _that = this
				_that.om.request(
					"workEvaluateConfig/getMultiEvaUserResponse",
					{
						busCode:_that.busCode,
						userIdPosId: userIdPosId,
						bodyUserId:_that.bodyUserId
					}
				).then(res=>{
					if(_that.canEdit){
						let objArr = []
						let ids = res.value.evaMultiOffId.split(',')
						let names = res.value.evaMultiOffName.split(',')
						let posNames = res.value.evaMultiPosName.split(',')
						for(let index in ids){
							let obj = {
								checked: true,
								id: null,
								instId: null,
								instName: '',
								isUser: true,
								posId: null,
								posInstName:'',
								posName: '',
								username: ''
							}
							let userPosId = ids[index].split('/')
							obj.id = userPosId[0]
							obj.posId = userPosId[1]
							obj.username = names[index]
							obj.posName = posNames[index]
							objArr.push(obj)
						}
						_that.$refs.autoform.formdata.evaMultiPosName = res.value.evaMultiPosName
						uni.setStorage({
							key:_that.evaMultiOffDefaultDataCache,
							data:objArr
						})
					}
					_that.$refs.autoform.formdata.evaMultiOffId = res.value.evaMultiOffId
					_that.$refs.autoform.formdata.evaMultiOffName = res.value.evaMultiOffName
				})
			},
			clearDutyOff(){
				let _that = this
				_that.$refs.autoform.clear("title5","dutyOffId")
				uni.removeStorage({
					key:_that.defaultDutyOffDataCache
				})
			},
			setDefaultData(){
				let _that = this
				if(_that.formdata.dutyOffId){
					let idList = _that.formdata.dutyOffId.split(',')
					let nameList = _that.formdata.dutyOffName.split(',')
					let data = []
					for(let index in idList){
						let idMixList = idList[index].split('/')
						let nameMixList = nameList[index].split('/')
						let obj = {
							id:idMixList[0],
							username:nameMixList[0],
							posId:idMixList[1],
							posName:nameMixList[1]
						}
						data.push(obj)
					}
					uni.setStorage({
						key:'searchUserDefaultList',
						data:data
					})
				}	
			},
			onSelectUserPost(data) {
				let row = this.$refs.autoform.formdata
				row.userId = data.userId;
				row.userName = data.userName;
				row.postId = data.posId;
				row.postName = data.posName;
			},
			initLoad() {
				let _that = this
				changeEditor('title5', "dutyOffId", {
					'link': '../../public/searchUser/searchUser',
					"linkParam": {
						isNew:true,
						searchfield: "q",
						cachename: _that.dutyOffCacheKey,
						defaultDataCache:_that.defaultDutyOffDataCache,
						isUserPos:true,
						pagination: true
					}
				}, null, _that.autoformdata)
				changeEditor('title5', "evaluateOffId", {
					"link": "/pages/public/pageSearch/pageSearch",
					"linkParam": {
						namefield: "userPosInstName",
						searchfield: "q",
						url: urlUtil.officePageUrl().url,
						cachename: _that.evaluateOffCacheKey,
						placeholder: urlUtil.officePageUrl().prompt
					}
				}, null, _that.autoformdata)
				changeEditor('title5', "assistOffId", {
					"link": "/pages/public/pageSearch/pageSearch",
					"linkParam": {
						namefield: "userPosInstName",
						searchfield: "q",
						url: urlUtil.officePageUrl().url,
						cachename: _that.assistOffCacheKey,
						placeholder: urlUtil.officePageUrl().prompt
					}
				}, null, _that.autoformdata)
				let arr = [
					_that.om.request(
						"dataDict/getIntByCode", {
							code: 'DD021017008'
						}
					)
				]

				Promise.all(arr).then(res => {
					if (!(_that.$refs.autoform.formdata.score + '')) {
						_that.$refs.autoform.formdata.score = res[0].value
					}
					if(_that.formdata.dutyOffName){
						let arr = _that.formdata.dutyOffName.split(',')
						let user5Name = ''
						user5Name = arr.map(e=>e.split('/')[0]).join(',')
						_that.$refs.autoform.formdata.user5Name = user5Name
					}
				})
			}
		},
		onUnload(){
			let _that = this
			uni.removeStorage({
				key:_that.defaultDutyOffDataCache
			})
			uni.removeStorage({
				key:_that.evaMultiOffDefaultDataCache
			})
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:"dataDict/getIntByCode",
					param:{
						code:'DD021025001'
					}
				}
			]
			subOnLoad(_that, e, function(ajaxData) {
				_that.postId = e.postId
				if(e.busCode){
					_that.busCode = e.busCode
				}
				if(e.user2OffId){
					_that.bodyUserId = e.user2OffId
				}
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if (res.data && Object.prototype.toString.call(res.data) ===
								'[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								if(_that.formdata.dutyOffId){
									let objArr = []
									let ids = _that.formdata.dutyOffId.split(',')
									let names = _that.formdata.dutyOffName.split(',')
									let posNames = _that.formdata.dutyPosName.split(',')
									for(let index in ids){
										let obj = {
											checked: true,
											id: null,
											instId: null,
											instName: '',
											isUser: true,
											posId: null,
											posInstName:'',
											posName: '',
											username: ''
										}
										let userPosId = ids[index].split('/')
										obj.id = userPosId[0]
										obj.posId = userPosId[1]
										obj.username = names[index]
										obj.posName = posNames[index]
										objArr.push(obj)
									}
									uni.setStorage({
										key:_that.defaultDutyOffDataCache,
										data:objArr
									})
								}	
								if(ajaxData[0].value + '' == '0'){
									changeEditor("title5","evaMultiOffId", {
										'link':'../../public/searchUser/searchUser',
										linkParam: {
											cachename: _that.evaMultiOffCacheKey,
											defaultDataCache:_that.evaMultiOffDefaultDataCache,
											isNew:true,
											isUserPos:true,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true
										}
									}, null, _that.autoformdata);
								}else{
									_that.canEdit = false
									changeEditor("title5","evaMultiOffId",'type','labelbox',_that.autoformdata)
								}
								if(_that.formdata.evaMultiOffId && (ajaxData[0].value + '' == '0')){
									let objArr = []
									let ids = _that.formdata.evaMultiOffId.split(',')
									let names = _that.formdata.evaMultiOffName.split(',')
									let posNames = _that.formdata.evaMultiPosName.split(',')
									for(let index in ids){
										let obj = {
											checked: true,
											id: null,
											instId: null,
											instName: '',
											isUser: true,
											posId: null,
											posInstName:'',
											posName: '',
											username: ''
										}
										let userPosId = ids[index].split('/')
										obj.id = userPosId[0]
										obj.posId = userPosId[1]
										obj.username = names[index]
										obj.posName = posNames[index]
										objArr.push(obj)
									}
									uni.setStorage({
										key:_that.evaMultiOffDefaultDataCache,
										data:objArr
									})
								}
								_that.initLoad()
							}
						}
					});
				}
			},arr)
		},
		onShow() {
			let _that = this
			//_that.setDefaultData()
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: this.dutyOffCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							let formdata = _that.$refs.autoform.formdata;
							uni.setStorage({
								key:_that.defaultDutyOffDataCache,
								data:d
							})
							if(!formdata.dutyOffName){
								formdata.dutyOffName = ''
							}
							let idList = []
							let nameList = []
							let posNameList = []
							for(let index in d){
								idList.push(d[index].id + '/' + d[index].posId)
								nameList.push(d[index].username)
								posNameList.push(d[index].posName)
							}
							
							let idStr = idList.map(e =>e).join(",")
							let nameStr = nameList.map(e =>e).join(",");
							let posNameStr = posNameList.map(e =>e).join(",");
							
							formdata.dutyOffId = idStr
							formdata.dutyOffName = nameStr
							formdata.dutyPosName = posNameStr
							if(formdata.dutyOffId){
								_that.getMultiEvaUserResponse(formdata.dutyOffId)
							}
							uni.removeStorage({
								key: _that.dutyOffCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.evaMultiOffCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data;
							uni.setStorage({
								key:_that.evaMultiOffDefaultDataCache, 
								data:d
							})
							let formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.evaMultiOffId = idStr
								formdata.evaMultiOffName = nameStr
								formdata.evaMultiPosName = posNameStr
							}else{
								_that.$refs.autoform.clear("title5","evaMultiOffId")
							}
							uni.removeStorage({
								key: _that.evaMultiOffCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key: this.evaluateOffCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.evaluateOffId = d.id
							formdata.evaluateOffName = d.userName
							uni.removeStorage({
								key: _that.evaluateOffCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: this.assistOffCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.assistOffId = d.id
							formdata.assistOffName = d.userName
							uni.removeStorage({
								key: _that.assistOffCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>
